package cn.com.ctcloud.algorithm.paxos.main;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import cn.com.ctcloud.algorithm.paxos.doer.Acceptor;
import cn.com.ctcloud.algorithm.paxos.doer.Proposer;

/**
 * 主函数
 */
public class Main {
	// 提议者数量
	private static final int NUM_OF_PROPOSER = 5;
	// 决策者数量
	private static final int NUM_OF_ACCEPTOR = 7;
	public static CountDownLatch latch = new CountDownLatch(NUM_OF_PROPOSER);;

	public static void main(String[] args) {
		// 创建决策者
		List<Acceptor> acceptors = new ArrayList<>();
		for (int i = 0; i < NUM_OF_ACCEPTOR; i++) {
			acceptors.add(new Acceptor());
		}

		// 创建线程
		ExecutorService es = Executors.newCachedThreadPool();
		for (int i = 0; i < NUM_OF_PROPOSER; i++) {
			// 提议者
			Proposer proposer = new Proposer(i, i + "#Proposer", NUM_OF_PROPOSER, acceptors);
			es.submit(proposer);
		}
		es.shutdown();
	}
}
